<?php

class MyDB extends mysqli{
	 	
	/**
	 * Creates a new instance of mydb which extends from the MYSQLI class.
	 * @param <string> $host - the host of the database.
	 * @param <string> $user - the username for the database.
	 * @param <string> $pass - the password for the database.
	 * @param <string> $db - the name of the database.
	 */
	public function __construct($host, $user, $pass, $db){
        parent::__construct($host, $user, $pass, $db);

        if(mysqli_connect_error()){
            die('Connect Error('.mysqli_connect_errno().')'. mysqli_connect_error());
        }
    }
    
    /**
     * Returns an associative array containing all the rows from the query.
     * @param <string> $query - the query to perform on the database.
     */
    public function getAll($query){
    	if(!empty($query)){
    		$res = parent::query($query);
    		if(!$res){
    			echo 'Error in query:<br>'.$query.'<br>'.parent::$this->error.'<br>';
    		}elseif($res->num_rows > 0){
    			$output = '';
    			while($row = $res->fetch_assoc()){
    				$output[] = $row;
    			}
    			return $output;
    		}
    	}
    	return null;
    }
    
    /**
     * Returns an associative array containing one row from the query.
     * @param <string> $query - the query to perform on the database.
     */
    public function getRow($query){
    	if(!empty($query)){
    		$res = parent::query($query);
    		if(!$res){
    			echo 'Error in query:<br>'.$query.'<br>'.parent::$this->error.'<br>';
    		}elseif($res->num_rows > 0){
    			return $res->fetch_assoc();
    		}
    	}
    	return null;
    }
    
    /**
     * Returns an associative array containing a single value from the query.
     * @param <string> $query - the query to perform on the database.
     */
    public function getOne($query){
    	if(!empty($query)){
    		$res = parent::query($query);
    		if(!$res){
    			echo 'Error in query:<br>'.$query.'<br>'.parent::$this->error.'<br>';
    		}elseif($res->num_rows > 0){
    			$row = $res->fetch_row();
    			return $row[0];
    		}
    	}
    	return null;
    }
}

?>